package com.example.demo;

import java.io.File;
import java.io.IOException;
import java.util.List;
import org.noear.solon.annotation.*;
import org.noear.solon.core.handle.UploadedFile;

@Mapping("/rpc")
@Controller
public class HelloServiceImpl implements HelloService {
  @Override
  @Post
  @Mapping("hello")
  public String hello(String name) {
    return String.format("Hello %s!", name);
  }

  @Override
  @Mapping("/test01")
  @Post
  public String test01(@Param("ids") List<String> ids) {
    return String.join(",", ids);
  }

  @Override
  @Mapping("/test02")
  @Post
  public String test02(@Param("file") UploadedFile file) {
    File target =
        new File("/Users/airhead/WorkSpace/demo/solon-demo8/src/test/java/features/temp.gradle");
    try {
      file.transferTo(target);
    } catch (IOException e) {
      throw new RuntimeException(e);
    }

    return file.getName();
  }

  @Override
  @Mapping("/test03")
  @Post
  public String test03() {
    return "test03";
  }

  @Override
  @Mapping("/test04/{namex}")
  @Get
  public String test04(@Path("namex") String name) {
    return "hello " + name;
  }

  @Override
  @Mapping("/test05")
  @Post
  public String test05(HelloDto dto) {
    if (dto == null) {
      return "hello!";
    }

    return "hello " + dto.getName();
  }
}
